﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RibbonEditor.Controls.General
{
    public partial class HideCustomActionControl : UserControl, IControlRulable
    {
        private HideCustomAction action;

        internal HideCustomAction Action
        {
            get { return this.action; }
            set { this.action = value; }
        }

        public HideCustomActionControl()
        {
            InitializeComponent();

            this.action = new HideCustomAction();
        }

        public HideCustomActionControl(HideCustomAction action)
            : this()
        {
            this.action = action;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.strId.Value = this.action.Id;
                this.strLocation.Value = this.action.Location;
                this.icSequence.Value = this.action.Sequence;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.action.Id = this.strId.Value;
                    this.action.Location = this.strLocation.Value;
                    this.action.Sequence = this.icSequence.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void CommandDefinitionControl_Leave(object sender, EventArgs e)
        {
            try
            {
                bool isDirty = false;

                foreach (IDirtable d in this.Controls)
                {
                    isDirty = isDirty || d.IsDirty;
                }

                if (isDirty)
                {
                    if (DialogResult.Yes == MessageBox.Show(this, "You didn't save your changes! Do you want to save them now?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        this.Save();

                        foreach (IDirtable d in this.Controls)
                        {
                            d.IsDirty = false;
                        }
                    }
                    else
                    {
                        foreach (IDirtable d in this.Controls)
                        {
                            d.ReinitValues();
                        }
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.action.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
